package com.minio.starter;

import com.minio.starter.service.MinioService;
import com.minio.starter.service.impl.MinioServiceImpl;
import com.minio.starter.template.MinioTemplate;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author theOS
 * @date 2021/10/29
 */
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class NauMinioAutoConfiguration {
    @Autowired
    MinioProperties properties;

    @Bean
    public MinioClient getMinioClient() {
        return MinioClient.builder()
                .endpoint(properties.getEndpoint())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();
    }

    @Bean
    @ConditionalOnBean(MinioClient.class)
    public MinioTemplate getMinioTemplate() {
        return new MinioTemplate();
    }

    @Bean
    @ConditionalOnBean(MinioTemplate.class)
    public MinioService getMinioService() {
        return new MinioServiceImpl();
    }

}
